/**
 * Created by Administrator on 2016/11/4.
 */
/* global $,str_industryList */
'use strict'

window.kmads_fn = {}
/**
 * 检查字符串
 */

// function _getStrLength (str) {
//  var aMatch = str.match(/[^\x00-\x80]/g)
//  return Math.ceil((str.length + (!aMatch ? 0 : aMatch.length)) / 2)// 向上取整
// }

window.kmads_fn = {
    // 检查长度 maxLength:允许输入最长长度 inputID：要监听的输入框的ID waringID:提示的标签的ID
  checkInputLength: function (maxLength, inputID, waringID) {
    var str = $('#' + inputID).val()
    var length = str.length
    var content

    if (length <= maxLength) {
      content = length + '/' + maxLength
      $('#' + waringID).html(content).css('color', '#727d89')
    } else {
      $('#' + inputID).val($('#' + inputID).val().substring(0, maxLength))
      content = $('#' + inputID).val().length + '/' + maxLength
      $('#' + waringID).html(content)
    }
  },
    // 获取出文本框的初始长度，并放在提示信息里面
    // maxLength:允许输入最长长度 inputID：要监听的输入框的ID waringID:提示的标签的ID
  initLengthWaring: function (maxLength, inputID, waringID) {
    var str = $('#' + inputID).val()
    var length = str.length
    var content = length + '/' + maxLength
    $('#' + waringID).html(content)
  },

    // 初始化行业的选择框 p_lableid 父行业选择框ID  sub_lableid 子行业选择框id  industryList js中获取到的json字符串格式的行业数据
  initIndustry: function (lableid, subLableid, strIndustryList) {
    var objIndustryList = JSON.parse(strIndustryList)
        // console.log(obj_industryList);
    for (var i = 0; i < objIndustryList.length; i++) {
      var html = '<option class="p-industry p-industry-' + objIndustryList[i].id + '" data-id="' + objIndustryList[i].id + '">' + objIndustryList[i].name + '</option>'
      $('#' + lableid).append(html)
    }

        // 初始化子行业id
    var pid = $('#' + lableid + ' option:selected').attr('data-id')
    for (var n = 0; i < objIndustryList.length; n++) {
      if (pid === objIndustryList[i].id) {
        var subIndustry = objIndustryList[i].sub_industry
        for (var j = 0; j < subIndustry.length; j++) {
          html = '<option class="sub-industry sub-industry-' + subIndustry[j].sub_id + '" data-id="' + subIndustry[j].sub_id + '">' + subIndustry[j].sub_name + '</option>'
          $('#' + subLableid).append(html)
        }
        break
      }
    }
  },

    // 动态生成子行业列表 p_lableid 父行业选择框ID  sub_lableid 子行业选择框id  industryList js中获取到的json字符串格式的行业数据
  getSubIndustry: function (lableid, subLableid) {
    var objIndustryList = JSON.parse(str_industryList)
    var pid = $('#' + lableid + ' option:selected').attr('data-id')
    $('#' + subLableid).html('')
        // console.log(obj_industryList);
    for (var i = 0; i < objIndustryList.length; i++) {
      if (pid === objIndustryList[i].id) {
        var subIndustry = objIndustryList[i].sub_industry
        for (var j = 0; j < subIndustry.length; j++) {
          var html = '<option class="sub-industry sub-industry-' + subIndustry[j].sub_id + '" data-id="' + subIndustry[j].sub_id + '">' + subIndustry[j].sub_name + '</option>'
          $('#' + subLableid).append(html)
        }
        break
      }
    }
  },

    // 根据用户的信息设置用户之前选择的行业信息
  setIndustry: function (lableid, industryId, subLableid, subIndustryId) {
    $('#' + lableid + ' .p-industry-' + industryId).attr('selected', true)
    var objIndustryList = JSON.parse(str_industryList)
    $('#' + subLableid).html('')
        // console.log(obj_industryList);
    for (var i = 0; i < objIndustryList.length; i++) {
      if (industryId === objIndustryList[i].id) {
        var subIndustry = objIndustryList[i].sub_industry
        for (var j = 0; j < subIndustry.length; j++) {
          var html = '<option class="sub-industry sub-industry-' + subIndustry[j].sub_id + '" data-id="' + subIndustry[j].sub_id + '">' + subIndustry[j].sub_name + '</option>'
          $('#' + subLableid).append(html)
        }
        break
      }
    }
    $('#' + subLableid + ' .sub-industry-' + subIndustryId).attr('selected', true)
  }
}
